package com.ruoyi.consumer.controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.consumer.domain.CCompany;
import com.ruoyi.consumer.domain.CDeviceOrders;
import com.ruoyi.consumer.domain.COrder;
import com.ruoyi.consumer.domain.CUser;
import com.ruoyi.consumer.service.ICCompanyService;
import com.ruoyi.consumer.service.ICDeviceOrdersService;
import com.ruoyi.consumer.service.ICOrderService;
import com.ruoyi.consumer.service.ICUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.DateUtils.YYYY_MM_DD_HH_MM_SS;

/**
 * 支付订单Controller
 * 
 * @author ruoyi
 * @date 2023-11-19
 */
@RestController
@RequestMapping("/consumer/order")
public class COrderController extends BaseController
{
    @Autowired
    private ICOrderService cOrderService;

    @Autowired
    private ICCompanyService companyService;

    @Autowired
    private ICUserService userService;

    @Autowired
    private ICDeviceOrdersService deviceOrdersService;

    public TableDataInfo getDataTable(List<COrder> list, List<?> list1)
    {
        // 使用原始 list 创建 PageInfo 对象来保留分页信息
        PageInfo<COrder> pageInfo = new PageInfo<>(list);
        TableDataInfo rspData = new TableDataInfo(list1, (int)pageInfo.getTotal());
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list1);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    /**
     * 查询支付订单列表
     */
    @PreAuthorize("@ss.hasPermi('consumer:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(COrder cOrder)
    {
        startPage();
        List<COrder> list = cOrderService.selectCOrderList(cOrder);
        List<AjaxResult> ajaxResults = new ArrayList<>();
        for (COrder order : list) {
            AjaxResult ajaxResult = new AjaxResult();

            //将上面的属性赋值给ajaxResult
            ajaxResult.put("orderId", order.getOrderId());
            ajaxResult.put("deviceOrderId", order.getDeviceOrderId());
            ajaxResult.put("orderAmount", order.getOrderAmount());
            ajaxResult.put("orderMoney", order.getOrderMoney());
            ajaxResult.put("orderPay", order.getOrderPay());
            ajaxResult.put("orderRefund", order.getOrderRefund());
            ajaxResult.put("orderNum", order.getOrderNum());
            ajaxResult.put("dealType", order.getDealType());
            ajaxResult.put("orderRefundNum", order.getOrderRefundNum());
            ajaxResult.put("orderStatus", order.getOrderStatus());
            ajaxResult.put("dealAmount", order.getDealAmount());
            ajaxResult.put("createTime", DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS,order.getCreateTime()));
            CCompany company = companyService.selectCCompanyByCompanyId(order.getCompanyId());
            if (company != null) {
                ajaxResult.put("companyName", company.getCompanyName());
            } else {
                ajaxResult.put("companyName", "");
            }


            CUser cUser = userService.selectCUserByUserId(order.getTargetUserId());
            if (cUser != null) {
                ajaxResult.put("targetUserId", cUser.getUserName());
            } else {
                ajaxResult.put("targetUserId", "");
            }

            CDeviceOrders deviceOrders = deviceOrdersService.selectCDeviceOrdersByDeviceOrderId(order.getDeviceOrderId());
            if (deviceOrders != null) {
                ajaxResult.put("deviceOrderId", deviceOrders.getFillerId());
            } else {
                ajaxResult.put("deviceOrderId", "");
            }

            ajaxResults.add(ajaxResult);
        }
        return getDataTable(list,ajaxResults);
    }

    /**
     * 导出支付订单列表
     */
    @PreAuthorize("@ss.hasPermi('consumer:order:export')")
    @Log(title = "支付订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, COrder cOrder)
    {
        List<COrder> list = cOrderService.selectCOrderList(cOrder);
        ExcelUtil<COrder> util = new ExcelUtil<COrder>(COrder.class);
        util.exportExcel(response, list, "支付订单数据");
    }

    /**
     * 获取支付订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('consumer:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Integer orderId)
    {
        return success(cOrderService.selectCOrderByOrderId(orderId));
    }

    /**
     * 新增支付订单
     */
    @PreAuthorize("@ss.hasPermi('consumer:order:add')")
    @Log(title = "支付订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody COrder cOrder)
    {
        return toAjax(cOrderService.insertCOrder(cOrder));
    }

    /**
     * 修改支付订单
     */
    @PreAuthorize("@ss.hasPermi('consumer:order:edit')")
    @Log(title = "支付订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody COrder cOrder)
    {
        return toAjax(cOrderService.updateCOrder(cOrder));
    }

    /**
     * 删除支付订单
     */
    @PreAuthorize("@ss.hasPermi('consumer:order:remove')")
    @Log(title = "支付订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Integer[] orderIds)
    {
        return toAjax(cOrderService.deleteCOrderByOrderIds(orderIds));
    }
}
